package com.repast.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.repast.common.Const;
import com.repast.common.ServerResponse;
import com.repast.dao.DeskTimeMapper;
import com.repast.dao.OrdersMapper;
import com.repast.dao.UrgeOrdersMapper;
import com.repast.pojo.DeskTime;
import com.repast.pojo.Orders;
import com.repast.pojo.UrgeOrders;
import com.repast.service.IUrgeOrdersService;
import com.repast.utils.CalculateDateUtils;
import com.repast.utils.DateUtils;

@Service
public class UrgeOrderServiceImpl implements IUrgeOrdersService {
	
	@Autowired
	private UrgeOrdersMapper urgeOrdersMapper;
	@Autowired
	private OrdersMapper ordersMapper;
	@Autowired
	private DeskTimeMapper deskTimeMapper;
	
	public ServerResponse urgeOrders(String openid) throws Exception{
		//催单的订单号,催单的桌号,催单,默认状态0(未接收信息),最后一次催单的时间,现在默认只有堂食才能催单
		List<Orders> list = ordersMapper.selectOrdersListByOpenidAndOrdersType(openid,1);
		for (Orders orders : list) {
			List<DeskTime> deskTimeList = deskTimeMapper.selectDeskTimeById(orders.getDeskId());
			for (DeskTime deskTime : deskTimeList) {
				String start = deskTime.getDeskTimeStartTime();
				String end = deskTime.getDeskTimeEndTime();
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
				if(start!=null&&end!=null){
					Date startTime = sdf.parse(start);
					Date endTime = sdf.parse(end);
					Date date = new Date();
					if(date.after(startTime) && date.before(endTime)){
						UrgeOrders urge = new UrgeOrders();
						urge.setOrdersId(orders.getOrdersId());
						urge.setUrgeOrdersStatus(Const.UrgeStatus.NO_RECEIVE);
						urge.setUrgeOrdersCreateTime(DateUtils.formatDateTime(new Date(), "long"));
						urge.setUrgeOrdersUpdateTime(DateUtils.formatDateTime(new Date(), "long"));
						int insert = urgeOrdersMapper.insert(urge);
						if(insert!=0){
							return ServerResponse.createBySuccess("催菜成功,请稍等");
						}
					}
				}
			}
		}
		return ServerResponse.createByErrorMessage("你当前没有正在进行的堂食订单");
	}
}
